﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using UnityEngine;
using Random = System.Random;

namespace HsVoice
{
    public static class HsTools
    {
        static public T[] MergeBytes<T>(this T[] header, T[] first)
        {
            return header.Concat(first).ToArray();
        }

        static public T[] MergeBytes<T>(this T[] header, T[] first, T[] second)
        {
            return (header.Concat(first).Concat(second)).ToArray();
        }

        static public T[] MergeBytes<T>(this T[] header, T[] first, T[] second, T[] third)
        {
            return (header.Concat(first).Concat(second).Concat(third)).ToArray();
        }

        static public T[] MergeBytes<T>(this T[] header, T[] first, T[] second, T[] third, T[] fourth)
        {
            return (header.Concat(first).Concat(second).Concat(third).Concat(fourth)).ToArray();
        }

        static public T[] Sclie<T>(this T[] source, int len)
        {
            return Sclie(source, 0, len);
        }

        static public T[] Sclie<T>(this T[] source, int start, int len)
        {
            return source.Skip(start).Take(len).ToArray();
        }

        static public T[] SclieFormIndex<T>(this T[] source, int index)
        {
            return source[index..];
        }

        static public byte[] ToBitEndian(this byte[] buffer)
        {
            Array.Reverse(buffer);
            return buffer;
        }

        static public byte[] ToLittleEndian(this byte[] buffer)
        {
            Array.Reverse(buffer);
            return buffer;
        }

        static public int BytesToInt32(this byte[] buffer)
        {
            return BitConverter.ToInt32(buffer);
        }

        static public byte[] ToBytes(this string str)
        {
            return Encoding.UTF8.GetBytes(str);
        }

        static public byte[] ToBytes(this int inter)
        {
            return BitConverter.GetBytes(inter);
        }

        static public byte[] ToGzip(this byte[] data)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    gzipStream.Write(data, 0, data.Length);
                }

                return memoryStream.ToArray();
            }
        }

        static public byte[] ToGUnzip(this byte[] compressedData)
        {
            using (MemoryStream memoryStream = new MemoryStream(compressedData))
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (MemoryStream decompressedStream = new MemoryStream())
                    {
                        gzipStream.CopyTo(decompressedStream);
                        return decompressedStream.ToArray();
                    }
                }
            }
        }


        private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        static public string NewTaskUid()
        {
            StringBuilder stringBuilder = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                stringBuilder.Append(chars[random.Next(chars.Length)]);
            }

            return stringBuilder.ToString();
        }

        static public string PlatformMP3Path(string fName)
        {

#if UNITY_ANDROID && !UNITY_EDITOR
            return $"file://{Application.persistentDataPath}/voice/{fName}.mp3";
#else
            return $"{Application.persistentDataPath}/voice/{fName}.mp3";
#endif
        }

        static public bool IsExist(int taskid)
        {
            return File.Exists($"{Application.persistentDataPath}/voice/{taskid}.mp3");
        }
    }
}